package com.niu.crm.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.niu.crm.core.ResponseObject;
import com.niu.crm.form.StudentSearchForm;
import com.niu.crm.model.Exam;
import com.niu.crm.model.User;
import com.niu.crm.service.ExamService;

@Controller
@RequestMapping(value = "/exam")
public class ExamController extends BaseController{
	@Autowired
    private ExamService examService;
	
	@RequestMapping(value = "/list.do")
    public String list(Model model){
		User user = this.getCurrentUser();
		
		return "exam/exam_list";
	}
	
	@RequestMapping(value = "/listData.do")
    @ResponseBody
    public Map<String,Object> listData(StudentSearchForm form, Pageable pager){
		User user = this.getCurrentUser();
		
		Map<String,Object> map = new java.util.HashMap<String, Object>();
		List<Exam> ls = null;

		ls = examService.loadAll();

		
		map.put("total", ls.size() );
		map.put("rows", ls);
		
		return map;
	}
	
	@RequestMapping(value = "/saveExam.do")
    @ResponseBody
    public ResponseObject<Boolean> saveExam(Exam exam){
		User user = this.getCurrentUser();
		
		if(exam.getId() == null){
			exam.setCreatorId(user.getId());
			examService.add(exam);
		}else{
			examService.update(exam);
		}

		return new ResponseObject<>(Boolean.TRUE);
	}
}
